package com.mymail.entity;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.mymail.dao.MailDAO;

public class Mail {
	// 邮件类型,,(收A),(已发B),(未发C)' ,
	public static final char SHOU = 'A';
	public static final char YIFA = 'B';
	public static final char WEIFA = 'C';

	private Integer id;
	private String subject;
	private Integer folder;
	private String per_name;
	private String from;
	private String content;
	private char sort = WEIFA;
	private ArrayList<Receiver> receiver;
	private Date sendTime;

	// private static MailDAO dao = new MailDAO();

	public MimeMessage getMimeMessage(Session session) {
		MimeMessage message = new MimeMessage(session);
		try {
			setSendTime(new Date());
			// message.set
			message.setSubject(getSubject());
			// message.setText(getContent());
			/*
			 * Multipart mp=new MimeMultipart(); BodyPart bp=new MimeBodyPart();
			 * bp.setContent(getContent(),"text/html;charset=UTF-8");
			 * mp.addBodyPart(bp); message.setContent(mp);
			 */
			message.setContent(getContent(), "text/html;charset=UTF-8");
			message.setSentDate(getSendTime());
			message.setFrom(new InternetAddress(getFrom(), "MyMail"));
			Iterator<Receiver> it = getReceiver().iterator();
			while (it.hasNext()) {
				message.addRecipient(Message.RecipientType.TO,
						new InternetAddress(it.next().getName()));
			}
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return message;
	}

	public void toDataBase() {
		setSort(YIFA);
		if (getDAO().addNew(this)) {
			Iterator<Receiver> it = getReceiver().iterator();
			while (it.hasNext()) {
				Receiver rec = it.next();
				rec.setMail(getId());
				Receiver.getDAO().addNew(rec);
			}
		} else {
			System.out.println("INSERT MAIL ERROR");
		}
	}

	public void addReceiver(Receiver rec) {
		if (receiver == null)
			receiver = new ArrayList<Receiver>();
		rec.setMail(getId());
		receiver.add(rec);
	}

	public static MailDAO getDAO() {
		return new MailDAO();
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = "<html><head></head><body>" + content
		+ "</body></html>";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject
	 *            the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the folder
	 */
	public Integer getFolder() {
		return folder;
	}

	/**
	 * @param folder
	 *            the folder to set
	 */
	public void setFolder(Integer folder) {
		this.folder = folder;
	}

	/**
	 * @return the per_name
	 */
	public String getPer_name() {
		return per_name;
	}

	/**
	 * @param perName
	 *            the per_name to set
	 */
	public void setPer_name(String perName) {
		per_name = perName;
	}

	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * @param from
	 *            the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * @return the sort
	 */
	public char getSort() {
		return sort;
	}

	/**
	 * @param sort
	 *            the sort to set
	 */
	public void setSort(char sort) {
		this.sort = sort;
	}

	/**
	 * @return the receiver
	 */
	public ArrayList<Receiver> getReceiver() {
		if (receiver == null)
			receiver = Receiver.getDAO().getReceivers(getId());
		return receiver;
	}

	/**
	 * @param receiver
	 *            the receiver to set
	 */
	public void setReceiver(ArrayList<Receiver> receiver) {
		this.receiver = receiver;
	}

	/**
	 * @return the sendTime
	 */
	public Date getSendTime() {
		return sendTime;
	}

	/**
	 * @param sendTime
	 *            the sendTime to set
	 */
	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}

}
